package com.tec.modules.order.rest;

import com.tec.modules.order.domain.OrderDetail;
import com.tec.modules.order.service.OrderDetailService;
import com.tec.modules.order.service.dto.OrderDetailQueryCriteria;
import me.zhengjie.annotation.Log;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author tec
* @date 2020-05-27
*/
@Api(tags = "销售订单：订单明细")
@RestController
@RequestMapping("/api/orderDetail")
public class OrderDetailController {

    private final OrderDetailService orderDetailService;

    public OrderDetailController(OrderDetailService orderDetailService) {
        this.orderDetailService = orderDetailService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('orderDetail:list')")
    public void download(HttpServletResponse response, OrderDetailQueryCriteria criteria) throws IOException {
        orderDetailService.download(orderDetailService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询订单明细")
    @ApiOperation("查询订单明细")
    @PreAuthorize("@el.check('orderDetail:list')")
    public ResponseEntity<Object> getOrderDetails(OrderDetailQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(orderDetailService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增订单明细")
    @ApiOperation("新增订单明细")
    @PreAuthorize("@el.check('orderDetail:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody OrderDetail resources){
        return new ResponseEntity<>(orderDetailService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改订单明细")
    @ApiOperation("修改订单明细")
    @PreAuthorize("@el.check('orderDetail:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody OrderDetail resources){
        orderDetailService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除订单明细")
    @ApiOperation("删除订单明细")
    @PreAuthorize("@el.check('orderDetail:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        orderDetailService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
